﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace V2TEK.NET.SH.Silverlight.GanttView
{
    public class GanttOutput
    {
        public byte[] ToProjectXML(ObservableCollection<ITaskDescriptor> tasks)
        {
            string company = "V2TEK";
            ITaskDescriptor[] task = GetTasks(tasks, null);
            int _taskCount = task.Length;//当前任务对象的个数
            //int _assignmentCount = task.ElementAt<ITaskDescriptor>(0).Project.Assignments.Count();//任务与资源存在对应关系的个数
            #region timescale for instance

            #endregion
            #region Calendars

            #endregion
            #region TreeTable


            #endregion
            var result = from c in tasks where c.HasStartDate select c.StartDate;
            var prjStart = result.Min();
            result = from c in tasks where c.HasEndDate select c.EndDate;
            var prjEnd = result.Max();

            #region transfrom .xml
            StringBuilder sb = new StringBuilder();
            try
            {
                #region Head
                sb.AppendFormat(@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>");
                sb.AppendLine(); sb.AppendFormat(@"<Project xmlns=""http://schemas.microsoft.com/project"">");
                sb.AppendLine(); sb.AppendFormat(@"	<SaveVersion>12</SaveVersion>");
                sb.AppendLine(); sb.AppendFormat(@"	<Name>{0}.xml</Name>", task[0].TaskName);
                sb.AppendLine(); sb.AppendFormat(@"	<Company>{0}</Company>", company);
                sb.AppendLine(); sb.AppendFormat(@"	<CreationDate>{0}</CreationDate>", System.DateTime.Now.ToString("s"));
                sb.AppendLine(); sb.AppendFormat(@"	<LastSaved>{0}</LastSaved>", System.DateTime.Now.ToString("s"));
                sb.AppendLine(); sb.AppendFormat(@"	<ScheduleFromStart>1</ScheduleFromStart>");
                sb.AppendLine(); sb.AppendFormat(@"	<StartDate>{0}</StartDate>", prjStart.ToString("s"));
                sb.AppendLine(); sb.AppendFormat(@"	<FinishDate>{0}</FinishDate>", prjEnd.ToString("s"));
                sb.AppendLine(); sb.AppendFormat(@"	<FYStartDate>1</FYStartDate>");
                sb.AppendLine(); sb.AppendFormat(@"	<CriticalSlackLimit>0</CriticalSlackLimit>");
                sb.AppendLine(); sb.AppendFormat(@"	<CurrencyDigits>2</CurrencyDigits>");
                sb.AppendLine(); sb.AppendFormat(@"	<CurrencySymbol>$</CurrencySymbol>");
                sb.AppendLine(); sb.AppendFormat(@"	<CurrencyCode>CNY</CurrencyCode>");
                sb.AppendLine(); sb.AppendFormat(@"	<CurrencySymbolPosition>0</CurrencySymbolPosition>");
                sb.AppendLine(); sb.AppendFormat(@"	<CalendarUID>1</CalendarUID>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultStartTime>00:00:00</DefaultStartTime>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultFinishTime>0:00:00</DefaultFinishTime>");
                sb.AppendLine(); sb.AppendFormat(@"	<MinutesPerDay>1440</MinutesPerDay>");//每日工时
                sb.AppendLine(); sb.AppendFormat(@"	<MinutesPerWeek>2400</MinutesPerWeek>");
                sb.AppendLine(); sb.AppendFormat(@"	<DaysPerMonth>20</DaysPerMonth>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultTaskType>0</DefaultTaskType>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultFixedCostAccrual>3</DefaultFixedCostAccrual>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultStandardRate>0</DefaultStandardRate>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultOvertimeRate>0</DefaultOvertimeRate>");
                sb.AppendLine(); sb.AppendFormat(@"	<DurationFormat>7</DurationFormat>");
                sb.AppendLine(); sb.AppendFormat(@"	<WorkFormat>2</WorkFormat>");
                sb.AppendLine(); sb.AppendFormat(@"	<EditableActualCosts>0</EditableActualCosts>");
                sb.AppendLine(); sb.AppendFormat(@"	<HonorConstraints>0</HonorConstraints>");
                sb.AppendLine(); sb.AppendFormat(@"	<InsertedProjectsLikeSummary>1</InsertedProjectsLikeSummary>");
                sb.AppendLine(); sb.AppendFormat(@"	<MultipleCriticalPaths>0</MultipleCriticalPaths>");
                sb.AppendLine(); sb.AppendFormat(@"	<NewTasksEffortDriven>1</NewTasksEffortDriven>");
                sb.AppendLine(); sb.AppendFormat(@"	<NewTasksEstimated>1</NewTasksEstimated>");
                sb.AppendLine(); sb.AppendFormat(@"	<SplitsInProgressTasks>1</SplitsInProgressTasks>");
                sb.AppendLine(); sb.AppendFormat(@"	<SpreadActualCost>0</SpreadActualCost>");
                sb.AppendLine(); sb.AppendFormat(@"	<SpreadPercentComplete>0</SpreadPercentComplete>");
                sb.AppendLine(); sb.AppendFormat(@"	<TaskUpdatesResource>1</TaskUpdatesResource>");
                sb.AppendLine(); sb.AppendFormat(@"	<FiscalYearStart>0</FiscalYearStart>");
                sb.AppendLine(); sb.AppendFormat(@"	<WeekStartDay>0</WeekStartDay>");
                sb.AppendLine(); sb.AppendFormat(@"	<MoveCompletedEndsBack>0</MoveCompletedEndsBack>");
                sb.AppendLine(); sb.AppendFormat(@"	<MoveRemainingStartsBack>0</MoveRemainingStartsBack>");
                sb.AppendLine(); sb.AppendFormat(@"	<MoveRemainingStartsForward>0</MoveRemainingStartsForward>");
                sb.AppendLine(); sb.AppendFormat(@"	<MoveCompletedEndsForward>0</MoveCompletedEndsForward>");
                sb.AppendLine(); sb.AppendFormat(@"	<BaselineForEarnedValue>0</BaselineForEarnedValue>");
                sb.AppendLine(); sb.AppendFormat(@"	<AutoAddNewResourcesAndTasks>1</AutoAddNewResourcesAndTasks>");
                sb.AppendLine(); sb.AppendFormat(@"	<CurrentDate>{0}</CurrentDate>", DateTime.Now.ToString("s"));
                sb.AppendLine(); sb.AppendFormat(@"	<MicrosoftProjectServerURL>1</MicrosoftProjectServerURL>");
                sb.AppendLine(); sb.AppendFormat(@"	<Autolink>1</Autolink>");
                sb.AppendLine(); sb.AppendFormat(@"	<NewTaskStartDate>0</NewTaskStartDate>");
                sb.AppendLine(); sb.AppendFormat(@"	<DefaultTaskEVMethod>0</DefaultTaskEVMethod>");
                sb.AppendLine(); sb.AppendFormat(@"	<ProjectExternallyEdited>0</ProjectExternallyEdited>");
                sb.AppendLine(); sb.AppendFormat(@"	<ExtendedCreationDate>1984-01-01T00:00:00</ExtendedCreationDate>");
                sb.AppendLine(); sb.AppendFormat(@"	<ActualsInSync>1</ActualsInSync>");
                sb.AppendLine(); sb.AppendFormat(@"	<RemoveFileProperties>0</RemoveFileProperties>");
                sb.AppendLine(); sb.AppendFormat(@"	<AdminProject>0</AdminProject>");
                sb.AppendLine(); sb.AppendFormat(@"	<OutlineCodes/>");
                sb.AppendLine(); sb.AppendFormat(@"	<WBSMasks/>");
                sb.AppendLine(); sb.AppendFormat(@"	<ExtendedAttributes/>");
                #endregion
                #region Caledar
                sb.AppendLine(); sb.AppendFormat(@"	<Calendars>");
                sb.AppendLine(); sb.AppendFormat(@"		<Calendar>");
                sb.AppendLine(); sb.AppendFormat(@"			<UID>1</UID>");
                sb.AppendLine(); sb.AppendFormat(@"			<Name>标准</Name>");
                sb.AppendLine(); sb.AppendFormat(@"			<IsBaseCalendar>1</IsBaseCalendar>");
                sb.AppendLine(); sb.AppendFormat(@"			<BaseCalendarUID>-1</BaseCalendarUID>");
                sb.AppendLine(); sb.AppendFormat(@"			<WeekDays>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>1</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>2</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>3</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>4</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>5</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>6</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"				<WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayType>7</DayType>");
                sb.AppendLine(); sb.AppendFormat(@"					<DayWorking>1</DayWorking>");
                sb.AppendLine(); sb.AppendFormat(@"					<WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"						<WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<FromTime>00:00:00</FromTime>");
                sb.AppendLine(); sb.AppendFormat(@"							<ToTime>0:00:00</ToTime>");
                sb.AppendLine(); sb.AppendFormat(@"						</WorkingTime>");
                sb.AppendLine(); sb.AppendFormat(@"					</WorkingTimes>");
                sb.AppendLine(); sb.AppendFormat(@"				</WeekDay>");
                sb.AppendLine(); sb.AppendFormat(@"			</WeekDays>");
                sb.AppendLine(); sb.AppendFormat(@"		</Calendar>");
                sb.AppendLine(); sb.AppendFormat(@"	</Calendars>");
                #endregion
                #region Task
                sb.AppendLine(); sb.AppendFormat(@"	<Tasks>");


                sb.AppendLine();
                sb.AppendFormat(@"		<Task>
			<UID>0</UID>
			<ID>0</ID>
			<Type>1</Type>
			<IsNull>0</IsNull>
			<CreateDate>{0}</CreateDate>
			<WBS>0</WBS>
			<OutlineNumber>0</OutlineNumber>
			<OutlineLevel>0</OutlineLevel>
			<Priority>500</Priority>
			<Start>{0}</Start>
			<Finish>{2}</Finish>
			<Duration>PT{3}H0M0S</Duration>
			<DurationFormat>53</DurationFormat>
			<Work>PT0H0M0S</Work>
			<ResumeValid>0</ResumeValid>
			<EffortDriven>0</EffortDriven>
			<Recurring>0</Recurring>
			<OverAllocated>0</OverAllocated>
			<Estimated>0</Estimated>
			<Milestone>0</Milestone>
			<Summary>1</Summary>
			<Critical>1</Critical>
			<IsSubproject>0</IsSubproject>
			<IsSubprojectReadOnly>0</IsSubprojectReadOnly>
			<ExternalTask>0</ExternalTask>
			<EarlyStart>{1}</EarlyStart>
			<EarlyFinish>{2}</EarlyFinish>
			<LateStart>{1}</LateStart>
			<LateFinish>{2}</LateFinish>
			<StartVariance>0</StartVariance>
			<FinishVariance>0</FinishVariance>
			<WorkVariance>0</WorkVariance>
			<FreeSlack>0</FreeSlack>
			<TotalSlack>0</TotalSlack>
			<FixedCost>0</FixedCost>
			<FixedCostAccrual>3</FixedCostAccrual>
			<PercentComplete>0</PercentComplete>
			<PercentWorkComplete>0</PercentWorkComplete>
			<Cost>0</Cost>
			<OvertimeCost>0</OvertimeCost>
			<OvertimeWork>PT0H0M0S</OvertimeWork>
			<ActualDuration>PT0H0M0S</ActualDuration>
			<ActualCost>0</ActualCost>
			<ActualOvertimeCost>0</ActualOvertimeCost>
			<ActualWork>PT0H0M0S</ActualWork>
			<ActualOvertimeWork>PT0H0M0S</ActualOvertimeWork>
			<RegularWork>PT0H0M0S</RegularWork>
			<RemainingDuration>PT24H0M0S</RemainingDuration>
			<RemainingCost>0</RemainingCost>
			<RemainingWork>PT0H0M0S</RemainingWork>
			<RemainingOvertimeCost>0</RemainingOvertimeCost>
			<RemainingOvertimeWork>PT0H0M0S</RemainingOvertimeWork>
			<ACWP>0</ACWP>
			<CV>0</CV>
			<ConstraintType>0</ConstraintType>
			<CalendarUID>-1</CalendarUID>
			<LevelAssignments>1</LevelAssignments>
			<LevelingCanSplit>1</LevelingCanSplit>
			<LevelingDelay>0</LevelingDelay>
			<LevelingDelayFormat>8</LevelingDelayFormat>
			<IgnoreResourceCalendar>0</IgnoreResourceCalendar>
			<HideBar>0</HideBar>
			<Rollup>0</Rollup>
			<BCWS>0</BCWS>
			<BCWP>0</BCWP>
			<PhysicalPercentComplete>0</PhysicalPercentComplete>
			<EarnedValueMethod>0</EarnedValueMethod>
			<IsPublished>1</IsPublished>
			<CommitmentType>0</CommitmentType>
		</Task>", DateTime.Now.ToString("s"), prjStart.ToString("s"), prjEnd.ToString("s"), (prjEnd - prjStart).TotalHours.ToString());

                for (int i = 0; i < task.Length; i++)
                {
                    ITaskDescriptor tsk = task[i];
                    sb.AppendLine(); sb.AppendFormat(@"		<Task>");
                    sb.AppendLine(); sb.AppendFormat(@"			<UID>{0}</UID>", tsk.UniqueId);
                    sb.AppendLine(); sb.AppendFormat(@"			<ID>{0}</ID>", i + 1);
                    sb.AppendLine(); sb.AppendFormat(@"			<Name>{0}</Name>", tsk.TaskName);
                    sb.AppendLine(); sb.AppendFormat(@"			<Type>0</Type>");
                    sb.AppendLine(); sb.AppendFormat(@"			<IsNull>0</IsNull>");
                    sb.AppendLine(); sb.AppendFormat(@"			<CreateDate>{0}</CreateDate>", DateTime.Now.ToString("s"));
                    sb.AppendLine(); sb.AppendFormat(@"			<WBS>{0}</WBS>", tsk.WBS);
                    sb.AppendLine(); sb.AppendFormat(@"			<OutlineNumber>{0}</OutlineNumber>", tsk.WBS);
                    sb.AppendLine(); sb.AppendFormat(@"			<OutlineLevel>{0}</OutlineLevel>", tsk.WBS == "0" ? 0 : tsk.WBS.Split('.').Length);
                    sb.AppendLine(); sb.AppendFormat(@"			<Priority>500</Priority>");
                    if (tsk.HasStartDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<Start>{0}</Start>", tsk.StartDate.ToString("s"));
                    }
                    if (tsk.HasEndDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<Finish>{0}</Finish>", tsk.EndDate.ToString("s"));
                    }
                    if (tsk.HasStartDate && tsk.HasEndDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<Duration>PT{0}H0M0S</Duration>", (TimeSpan.FromMilliseconds(tsk.TotalMilliseconds)).TotalHours.ToString());
                    }
                    sb.AppendLine(); sb.AppendFormat(@"			<DurationFormat>7</DurationFormat>");
                    sb.AppendLine(); sb.AppendFormat(@"			<Work>PT0H0M0S</Work>");

                    sb.AppendLine(); sb.AppendFormat(@"			<ResumeValid>1</ResumeValid>");
                    sb.AppendLine(); sb.AppendFormat(@"			<EffortDriven>{0}</EffortDriven>", (tsk.WBS.Split('.').Length == 2) ? 0 : 1);
                    sb.AppendLine(); sb.AppendFormat(@"			<Recurring>0</Recurring>");
                    sb.AppendLine(); sb.AppendFormat(@"			<OverAllocated>0</OverAllocated>");
                    sb.AppendLine(); sb.AppendFormat(@"			<Estimated>0</Estimated>");
                    sb.AppendLine(); sb.AppendFormat(@"			<Milestone>{0}</Milestone>", (tsk.StartDate == tsk.EndDate) ? 1 : 0);
                    sb.AppendLine(); sb.AppendFormat(@"			<Summary>{0}</Summary>", tsk.Children.Count > 0 ? 1 : 0);
                    sb.AppendLine(); sb.AppendFormat(@"			<Critical>1</Critical>");
                    sb.AppendLine(); sb.AppendFormat(@"			<IsSubproject>0</IsSubproject>");
                    sb.AppendLine(); sb.AppendFormat(@"			<IsSubprojectReadOnly>0</IsSubprojectReadOnly>");
                    sb.AppendLine(); sb.AppendFormat(@"			<ExternalTask>0</ExternalTask>");
                    if (tsk.HasStartDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<EarlyStart>{0}</EarlyStart>", tsk.StartDate.ToString("s"));
                    }
                    if (tsk.HasEndDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<EarlyFinish>{0}</EarlyFinish>", tsk.EndDate.ToString("s"));
                    }
                    if (tsk.HasStartDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<LateStart>{0}</LateStart>", tsk.StartDate.ToString("s"));
                    }
                    if (tsk.HasEndDate)
                    {
                        sb.AppendLine(); sb.AppendFormat(@"			<LateFinish>{0}</LateFinish>", tsk.EndDate.ToString("s"));
                    }
                    sb.AppendLine(); sb.AppendFormat(@"			<StartVariance>0</StartVariance>");
                    sb.AppendLine(); sb.AppendFormat(@"			<FinishVariance>0</FinishVariance>");
                    sb.AppendLine(); sb.AppendFormat(@"			<WorkVariance>0</WorkVariance>");
                    sb.AppendLine(); sb.AppendFormat(@"			<FreeSlack>0</FreeSlack>");
                    sb.AppendLine(); sb.AppendFormat(@"			<TotalSlack>0</TotalSlack>");
                    sb.AppendLine(); sb.AppendFormat(@"			<FixedCost>0</FixedCost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<FixedCostAccrual>3</FixedCostAccrual>");
                    sb.AppendLine(); sb.AppendFormat(@"			<PercentComplete>{0}</PercentComplete>", tsk.PercentComplete);
                    sb.AppendLine(); sb.AppendFormat(@"			<PercentWorkComplete>0</PercentWorkComplete>");
                    sb.AppendLine(); sb.AppendFormat(@"			<Cost>0</Cost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<OvertimeCost>0</OvertimeCost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<OvertimeWork>PT0H0M0S</OvertimeWork>");

                    sb.AppendLine(); sb.AppendFormat(@"			<ActualDuration>PT0H0M0S</ActualDuration>");
                    sb.AppendLine(); sb.AppendFormat(@"			<ActualCost>0</ActualCost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<ActualOvertimeCost>0</ActualOvertimeCost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<ActualWork>PT0H0M0S</ActualWork>");
                    sb.AppendLine(); sb.AppendFormat(@"			<ActualOvertimeWork>PT0H0M0S</ActualOvertimeWork>");

                    sb.AppendLine(); sb.AppendFormat(@"			<RegularWork>PT0H0M0S</RegularWork>");

                    sb.AppendLine(); sb.AppendFormat(@"			<RemainingDuration>PT6120H0M0S</RemainingDuration>");
                    sb.AppendLine(); sb.AppendFormat(@"			<RemainingCost>0</RemainingCost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<RemainingWork>PT0H0M0S</RemainingWork>");
                    sb.AppendLine(); sb.AppendFormat(@"			<RemainingOvertimeCost>0</RemainingOvertimeCost>");
                    sb.AppendLine(); sb.AppendFormat(@"			<RemainingOvertimeWork>PT0H0M0S</RemainingOvertimeWork>");


                    sb.AppendLine(); sb.AppendFormat(@"			<ACWP>0</ACWP>");
                    sb.AppendLine(); sb.AppendFormat(@"			<CV>0</CV>");
                    if (tsk.HasStartDate)
                    {
                        //关键的地方。ConstraintType=4表示不得早于....开始。默认越早越好，则会提前到项目开始时间。也可用前置任务来自动链接
                        sb.AppendLine(); sb.AppendFormat(@"			<ConstraintType>4</ConstraintType>");
                        sb.AppendLine(); sb.AppendFormat(@"			<CalendarUID>-1</CalendarUID>");
                        sb.AppendLine(); sb.AppendFormat(@"			<ConstraintDate>{0}</ConstraintDate>", tsk.StartDate.ToString("s"));
                    }
                    sb.AppendLine(); sb.AppendFormat(@"			<LevelAssignments>1</LevelAssignments>");
                    sb.AppendLine(); sb.AppendFormat(@"			<LevelingCanSplit>1</LevelingCanSplit>");
                    sb.AppendLine(); sb.AppendFormat(@"			<LevelingDelay>0</LevelingDelay>");
                    sb.AppendLine(); sb.AppendFormat(@"			<LevelingDelayFormat>8</LevelingDelayFormat>");
                    sb.AppendLine(); sb.AppendFormat(@"			<IgnoreResourceCalendar>0</IgnoreResourceCalendar>");
                    sb.AppendLine(); sb.AppendFormat(@"			<HideBar>0</HideBar>");
                    sb.AppendLine(); sb.AppendFormat(@"			<Rollup>0</Rollup>");
                    sb.AppendLine(); sb.AppendFormat(@"			<BCWS>0</BCWS>");
                    sb.AppendLine(); sb.AppendFormat(@"			<BCWP>0</BCWP>");
                    sb.AppendLine(); sb.AppendFormat(@"			<PhysicalPercentComplete>0</PhysicalPercentComplete>");
                    sb.AppendLine(); sb.AppendFormat(@"			<EarnedValueMethod>0</EarnedValueMethod>");
                    sb.AppendLine(); sb.AppendFormat(@"			<IsPublished>1</IsPublished>");
                    sb.AppendLine(); sb.AppendFormat(@"			<CommitmentType>0</CommitmentType>");

                    //IEnumerable<PredecessorLink> pre = tsk.PredecessorLinks;
                    //foreach (PredecessorLink pl in pre)
                    //{
                    //    sb.AppendLine(@"<PredecessorLink>");
                    //    sb.AppendLine(); sb.AppendFormat(@"<PredecessorUID>{0}</PredecessorUID>", pl.PredecessorUID.ToString());
                    //    sb.AppendLine(@"<Type>1</Type>");
                    //    sb.AppendLine(@"<CrossProject>0</CrossProject>");
                    //    sb.AppendLine(@"<LinkLag>0</LinkLag>");
                    //    sb.AppendLine(@"<LagFormat>7</LagFormat>");
                    //    sb.AppendLine(@"</PredecessorLink>");
                    //}
                    sb.AppendLine(); sb.AppendFormat(@"		</Task>");

                }
                sb.AppendLine(); sb.AppendFormat(@"	</Tasks>");
                #endregion
                #region Resource
                sb.AppendLine(); sb.AppendFormat(@"	<Resources>");
                //IEnumerable<Resource> res = task[0].Project.Resources;
                //for (int t = 0; t < res.Count<Resource>(); t++)
                //{
                //    sb.AppendLine(@"<Resource>");
                //    sb.AppendLine(); sb.AppendFormat(@"<Name>{0}</Name>", res.ElementAt<Resource>(t).Name);
                //    sb.AppendLine(); sb.AppendFormat(@"<UID>{0}</UID>", res.ElementAt<Resource>(t).UID.ToString());
                //    sb.AppendLine(); sb.AppendFormat(@"<ID>{0}</ID>", res.ElementAt<Resource>(t).ID.ToString());
                //sb.AppendLine(@"</Resource>");
                //}
                sb.AppendLine(@"</Resources>");

                #endregion
                #region Assignments
                sb.AppendLine(@"<Assignments>");
                //IEnumerable<Assignment> ass = task[0].Project.Assignments;
                //for (int k = 0; k < _assignmentCount; k++)
                //{
                //    sb.AppendLine(@"<Assignment>");
                //    sb.AppendLine(); sb.AppendFormat(@"<TaskUID>{0}</TaskUID>", ass.ElementAt<Assignment>(k).TaskUID.ToString());
                //    sb.AppendLine(); sb.AppendFormat(@"<ResourceUID>{0}</ResourceUID>", ass.ElementAt<Assignment>(k).ResourceUID.ToString());
                //    sb.AppendLine(@"</Assignment>");
                //}
                sb.AppendLine(@"</Assignments>");

                #endregion
                sb.AppendLine(); sb.AppendFormat(@"</Project>");

            }

            finally
            {

            }
            #endregion

            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            return buffer;
        }

        /// <summary>
        /// 将任务返回为一个数组
        /// </summary>
        /// <param name="tasks"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public ITaskDescriptor[] GetTasks(ObservableCollection<ITaskDescriptor> tasks, List<ITaskDescriptor> result)
        {
            if (result == null)
                result = new List<ITaskDescriptor>();
            for (int i = 0; i < tasks.Count; i++)
            {
                result.Add(tasks[i]);
                GetTasks(tasks[i].Children, result);
            }
            return result.ToArray();
        }
    }
}
